import * as THREE from "three";
import { Shape, ShapeParams } from "./Shape";
import { PoissonDiskSampling } from "../poisson";
/**单独实现一个rect 是为了试验一个算法  ,我这个平面，应该还需要一个法向*/
export class rectShape extends Shape {
    declare params: {
        x: number;
        y: number;
        count: number; // 密度
    };
    pointPool:THREE.Vector2[]=[];
    posisson: PoissonDiskSampling;
    halfX:number;
    halfY:number;
    constructor(origin: THREE.Vector3, params: ShapeParams = {}) {
        super(origin, params);
        this.posisson= new PoissonDiskSampling(this.params.x,this.params.y,0.1)
        this.pointPool = this.posisson.sample(this.params.count)
        this.halfX = (this.params.x || 1) / 2;
        this.halfY = (this.params.y || 1) / 2;
    }

    getRandomPosition(): THREE.Vector3 {
        // const x = (Math.random() - 0.5) * (this.params.x || 1);
        // const y = (Math.random() - 0.5) * (this.params.y || 1);
        let p= this.pointPool.pop()!;
        if(!p){
            this.pointPool = this.posisson.sample(this.params.count)
            p= this.pointPool.pop()!;
        }

        return new THREE.Vector3(p.x -this.halfX, p.y -this.halfY, 0).add(this.origin);
    }

    getRandomNormal(): THREE.Vector3 {
        // 默认返回六个面的法线之一
        const faces = [
            new THREE.Vector3(1, 0, 0),
            new THREE.Vector3(-1, 0, 0),
            new THREE.Vector3(0, 1, 0),
            new THREE.Vector3(0, -1, 0),
            new THREE.Vector3(0, 0, 1),
            new THREE.Vector3(0, 0, -1),
        ];
        return faces[Math.floor(Math.random() * faces.length)];
    }
}
